﻿using System;
using System.Linq;
using Entities;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace EntityFramework
{
	[TestClass]
	public class EntityFrameworkTest
	{
		

		[TestMethod]
		public void TestNavigation()
		{
			var e = new Employee();
			var c = new Company();
			c.Employees.Add(e);
			Assert.AreEqual(c, e.Company1);
		}

		[TestMethod]
		public void LazyLoadingWorks()
		{
			Employee lynn;
			using (var db = new EntityFrameworkDemoEntities())
			{
				db.ContextOptions.LazyLoadingEnabled = false;
				lynn = (from p in db.Employees.Include("Company1")
				        where p.Name == "Lynn"
				        select p).First();
				Console.WriteLine(lynn.Company1.Name);
				db.Detach(lynn);
			}
		}
		[TestMethod]
		public void LazyLoadingStillWorks()
		{
			Employee lynn;
			using (var db = new EntityFrameworkDemoEntities())
			{
				db.ContextOptions.LazyLoadingEnabled = false;
				lynn = (from p in db.Employees.Include("Company1")
								where p.Name == "Lynn"
								select p).First();
			}
			Assert.AreEqual("Microsoft", lynn.Company1.Name);
		}
		[TestMethod]
		public void LazyLoadingDoesNotWork()
		{
			Employee lynn;
			using (var db = new EntityFrameworkDemoEntities())
			{
				db.ContextOptions.LazyLoadingEnabled = false;
				lynn = (from p in db.Employees.Include("Company1")
				        where p.Name == "Lynn"
				        select p).First();
			Assert.AreEqual("Microsoft", lynn.Company1.Name);
				db.Detach(lynn);
			}
			Assert.AreEqual("Microsoft", lynn.Company1.Name);
		}

	}
}